import xlwt

from WebDriver.ChromeWebDriver import ChromeWebDriver
from WebHandler.WebHandler import WebHandler
from WebSite.base.BaseWebSite import BaseWebSite


class BossWebSite(BaseWebSite):

    def __init__(self):
        super(BossWebSite,self).__init__()
        self.webDriver = ChromeWebDriver()
        self.data = []
        self.detail_data = []

    def loadData(self):
        self.webDriver.loadUrl("https://www.zhipin.com/job_detail/?query=android&city=101280600&industry=&position=&page=1")

    def loadPage(self, page):
        self.webDriver.loadUrl("https://www.zhipin.com/job_detail/?query=android&city=101280600&industry=&position=&page=%s" % page)

    def loadAndParseDetailPage(self):
        index = 0
        size = len(self.data)
        for d in self.data:
            self.webDriver.loadUrl(d[self.KEY_DETAIL_URL])
            self.parseDetailData()
            if self.parseCallback is not None:
                self.parseCallback(index, size)
            index += 1
        pass

    def parseDetailData(self):
        info_primary = WebHandler(self.webDriver.getBrowser()).moveElementByClass("info-primary")
        job_sec = WebHandler(self.webDriver.getBrowser()).moveElementByClass("job-sec")
        sider_company = WebHandler(self.webDriver.getBrowser()).moveElementByClass("sider-company")
        dic = {}
        dic[self.KEY_COMPANY_NAME] = WebHandler(sider_company.getCurrentElement()).getAttrByXPathAt(attrName='title',xpath="//a[@ka='job-detail-company']",index=0).strip().replace('\n', '')
        dic[self.KEY_JOB_TITLE] = WebHandler(info_primary.getCurrentElement()).getTextByTagAt(tagName="h1",index=0)
        dic[self.KEY_SALARY] = WebHandler(info_primary.getCurrentElement()).getTextByClassAt(className="salary", index=0)
        dic[self.KEY_DETAIL_JOB_DESC] = WebHandler(job_sec.getCurrentElement()).getTextByClassAt(className="text",index=0)
        dic[self.KEY_DETAIL_URL] = self.webDriver.getBrowser().current_url
        self.detail_data.append(dic)
        pass

    def parseData(self):
        job_primary = WebHandler(self.webDriver.getBrowser()).moveElementByClass("job-primary")
        company_text = WebHandler(self.webDriver.getBrowser()).moveElementByClass("company-text")
        size = job_primary.getSize()


        for index in range(size):
            dic = {}
            dic[self.KEY_JOB_TITLE] = WebHandler(job_primary.getCurrentElement()).getTextByClassAt(className='job-title',index=index)
            dic[self.KEY_SALARY] = WebHandler(job_primary.getCurrentElement()).getTextByXPathAt(xpath="//span[@class='red']",index=index)
            dic[self.KEY_LOCATION] = WebHandler(job_primary.getCurrentElement()).getTextByTagAt(tagName="p", index=index)
            dic[self.KEY_DETAIL_URL] = WebHandler(job_primary.getCurrentElement()).getHrefByTagAt(tagName="a", index=index)

            dic[self.KEY_COMPANY_NAME] = WebHandler(company_text.getCurrentElement()).getTextByTagAt(tagName="a", index=index)
            dic[self.KEY_COMPANY_INFO] = WebHandler(company_text.getCurrentElement()).getTextByTagAt(tagName="p", index=index)
            dic[self.KEY_COMPANY_URL] = WebHandler(company_text.getCurrentElement()).getHrefByTagAt(tagName="a",index=index)
            if self.parseCallback is not None:
                self.parseCallback(index, size)
            self.data.append(dic)


    def print(self):
        for d in self.data:
            print(d)


    def saveExcel(self, filename):
        writebook = xlwt.Workbook()
        sheet = writebook.add_sheet('list')  # 在打开的excel中添加一个sheet
        line = 0
        for d in self.data:
            col = 0
            for key in d:
                if line == 0:
                    sheet.write(line, col, key)  # 写入excel，写入标题.
                else:
                    sheet.write(line, col, d[key])  # 写入excel，写入内容.
                col += 1
            if self.saveCallback is not None:
                self.saveCallback(line, len(self.data))
            line += 1
        writebook.save(filename)

    def saveDetailExcel(self, filename):
        writebook = xlwt.Workbook()
        sheet = writebook.add_sheet('detail')  # 在打开的excel中添加一个sheet
        line = 0
        for d in self.detail_data:
            col = 0
            for key in d:
                if line == 0:
                    sheet.write(line, col, key)  # 写入excel，写入标题.
                else:
                    sheet.write(line, col, d[key])  # 写入excel，写入内容.
                col += 1
            if self.saveCallback is not None:
                self.saveCallback(line, len(self.data))
            line += 1
        writebook.save(filename)